package demo2;

import demo1.IndexOutOfException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 邓大帅
 * Date: 2024-01-13
 * Time: 20:41
 */
public class MyLinkedList {
    static class LinkedNode {
        public int val;
        public LinkedNode prev;
        public LinkedNode next;

        public LinkedNode(int val) {
            this.val = val;
        }
    }

    public LinkedNode head;
    public LinkedNode last;

    public void display() {
        LinkedNode p = head;
        while (p != null) {
            System.out.print(p.val + " ");
            p = p.next;
        }
    }

    public int size() {
        LinkedNode p = head;
        int count = 0;
        while (p != null) {
            count++;
            p = p.next;
        }
        return count;
    }

    public boolean contains(int key) {
        LinkedNode p = head;
        while (p != null) {
            if (p.val == key) {
                return true;
            }
            p = p.next;
        }
        return false;
    }

    public void addFirst(int data) {
        LinkedNode linkedNode = new LinkedNode(data);
        if (this.head == null) {
            head = linkedNode;
            last = linkedNode;
            return;
        }
        head.prev = linkedNode;
        linkedNode.next = head;
        head = linkedNode;
    }

    public void addLast(int data) {
        LinkedNode linkedNode = new LinkedNode(data);
        if (this.head == null) {
            head = linkedNode;
            last = linkedNode;
            return;
        }
        last.next = linkedNode;
        linkedNode.prev = last;
        last = linkedNode;
    }

    public void addIndex(int pos, int data) throws IndexOutOfException {
        if (pos < 0 || pos > size()) {
            throw new IndexOutOfException();
        }
        if (pos == 0) {
            addFirst(data);
            return;
        }
        if (pos == this.size()) {
            addLast(data);
            return;
        }
        LinkedNode p = head;
        while (pos > 0) {
            p = p.next;
            pos--;
        }
        LinkedNode linkedNode = new LinkedNode(data);
        linkedNode.prev = p.prev;
        linkedNode.next = p;
        p.prev.next = linkedNode;
        p.prev = linkedNode;
    }
    public void remove(int key) {
        LinkedNode p = head;
        while (p != null) {
            if (p.val == key) {
                if (p == head) {
                    head = head.next;
                    if (head != null) {
                        head.prev = null;
                    }else {
                        last = null;
                    }
                }else {
                    p.prev.next = p.next;
                    if (p.next != null) {
                        p.next.prev = p.prev;
                    }else {
                        last = last.prev;
                    }
                }
                return;
            }
            p = p.next;
        }
    }
    public void removeAllKey(int key) {
        LinkedNode p = head;
        while (p != null) {
            if (p.val == key) {
                if (p == head) {
                    head = head.next;
                    if (head != null) {
                        head.prev = null;
                    }else {
                        last = null;
                    }
                }else {
                    p.prev.next = p.next;
                    if (p.next != null) {
                        p.next.prev = p.prev;
                    } else {
                        last = last.prev;
                    }
                }
            }
            p = p.next;
        }
    }
    public void clear() {
        LinkedNode p = head;
        while (p != null) {
            LinkedNode next = p.next;
            p.prev = null;
            p.next = null;
            p = next;
        }
        head = null;
        last = null;
    }
}
